import { ref } from 'vue'
import { defineStore } from 'pinia'
import {addOrderListAPI,payOrderAPI,acceptAPI,getOrderListAPI} from '@/API/order'
import { showToast } from 'vant'
import router from '@/router'
import {deleteOrderAPI} from '@/API/order'
export const useOrderStore = defineStore('order', () => {
 
const orderList = ref<any>([])
const orderId=ref<any>()

//获取订单列表
const getOrderList = async () => {
const res= await getOrderListAPI()
orderList.value=res.data.list

}

//新增订单数据
const addOrderList=async(products:any,address:any,totalMoney:any,discount:any,freightFee:any,pay:any) => {
  const res:any = await addOrderListAPI(products,address,totalMoney,discount,freightFee,pay)
  console.log(res);
  if(res.code===200){
    getOrderList()
   showToast('提交成功')
    setTimeout(()=>{
      router.push(`/payorder/${res.data.id}`)
    },2000)
  }
}
//订单支付
const payOrder=async(id:any,method:any)=>{
  const res:any = await payOrderAPI(id,method)
  if(res.code===200){
    getOrderList()
    showToast({
      type: 'success',
      message: '支付成功'
    })
    setTimeout(() =>{
      router.push('/paysuccess')
    },2000)
  }
}
//交易完成（收货）
const accept= async(id:any)=>{
  const res = await acceptAPI(id)
  console.log(res);
  
}

  const  deleteOrder=async()=>{
  const res = await deleteOrderAPI('5YlQyb88gYxDtgvL1bJ8h')
  
  console.log(res);
  getOrderList()
  }


  return { 
    orderList,
    orderId,
    addOrderList,
    payOrder,
    getOrderList,
    accept,
    deleteOrder
   }
},
{
  persist: true,
})


